﻿namespace Pyramid.Hardware.Gates
{
	using System;
	using System.Diagnostics;
	using System.Threading;

	/// <summary>
	/// Represents a logic gate that accepts a given number of input signals
	/// and produces a single output signal.
	/// </summary>
	public abstract class Gate
		: LineBase
	{
		private static int gateCount;

		protected Gate()
		{
			IncrementGateCount();
		}

		protected void WrapOutputLine(ILine outputLine)
		{
			outputLine.SignalChange += new EventHandler<SignalChangeEventArgs>((sender, e) => SetSignal(((ILine)sender).Signal));
			SetSignal(outputLine.Signal);
		}

		[Conditional("DEBUG")]
		private static void IncrementGateCount()
		{
			Debug.Print("{0} gates created.", Interlocked.Increment(ref gateCount));
		}
	}
}
